package com.snailhd.pipeline;

import com.snailhd.common.entity.IResult;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * 管道管理器
 * @author hd
 * @date 2019-11-23 12:56
 */
public class PipelineExecuteManager<T> {

    ExecutorService executor = Executors.newFixedThreadPool(4);

    public IResult exec(List<PipelineHandler> handlers,PipelineParams<T> args) throws Exception{

        List<PipelineFuture<IResult>> futures = new ArrayList<>(handlers.size());
        for (PipelineHandler handler:handlers){
            futures.add(handler.handle(args));
        }
        for (PipelineFuture<IResult> pipelineFuture: futures){
            IResult iResult = pipelineFuture.get();
            if(iResult.isFail()){
                return iResult;
            }
        }
        return new IResult();
    }

}
